import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/emojiOne/EmojiOneInfoes.dart';
import 'package:im_app/im/page/ext/MessageSelectionControls.dart';
import 'package:im_app/im/util/ImRichTextUtil.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/DateUtil.dart';
import '../../../../util/ImConstant.dart';
import '../../../bloc/MessageListCubit.dart';
import '../../../bloc/MessageSendCubit.dart';
import '../MessagePaneBackgroundView.dart';
import 'MessagePane.dart';


class RichTextMessagePane extends StatelessWidget {
  final WrapImMessage wrapMessage;

  const RichTextMessagePane({ required this.wrapMessage, super.key});

  @override
  Widget build(BuildContext context) {
    var messageSendCubit = context.read<MessageSendCubit>();
    var messageListCubit = context.read<MessageListCubit>();
    if (wrapMessage.message.messageType != ImMessageType.RichText) {
      throw Exception("消息类型不对${wrapMessage.message.messageType.name}");
    }

    List<String> contents = ImRichTextUtil.split(wrapMessage.message.content??'');
    List<InlineSpan> spans = contents.map((content) {
      if (content.startsWith(ImRichTextUtil.SPLIT)) {
        var shortName = content.substring(3, content.length-1);
        var emoji = EmojiOneInfoes.getInstance().getByUnicodeOrShortName(shortName);
        return WidgetSpan(child: Image(image: AssetImage(emoji?.emojiPath??''), width: 18.5, height: 18.5,));
      } else {
        return TextSpan(text: content);
      }

    }).toList(growable: false);

    // String time = DateUtil.formatDate(message.serverReceiveTime)??'';
    // spans.add(TextSpan(text: time));
    return MessagePaneBackgroundView(
      message: wrapMessage.message,
      child: SelectableText.rich(TextSpan(
        children: spans,
      ), selectionControls: MessageSelectionControls(message: wrapMessage.message, messageSendCubit: messageSendCubit, messageListCubit: messageListCubit), )
    );
  }
}

class RichTextMessageRefPane extends StatelessWidget {
  final ImMessage message;
  final MessageRefParam param;

  const RichTextMessageRefPane({ required this.message, required this.param, super.key});

  @override
  Widget build(BuildContext context) {
    if (message.messageType != ImMessageType.RichText) {
      throw Exception("消息类型不对" + message.messageType.name);
    }

    List<String> contents = ImRichTextUtil.split(message.content??'');
    List<InlineSpan> spans = contents.map((content) {
      if (content.startsWith(ImRichTextUtil.SPLIT)) {
        var shortName = content.substring(3, content.length-1);
        var emoji = EmojiOneInfoes.getInstance().getByUnicodeOrShortName(shortName);
        return WidgetSpan(child: Image(image: AssetImage(emoji?.emojiPath??''), width: param.iconSize, height: param.iconSize,));
      } else {
        return TextSpan(text: content, style: TextStyle(fontSize: param.fontSize));
      }

    }).toList(growable: false);

    return Padding(
        padding: const EdgeInsets.only(top: 2, right: 5, bottom: 2, left: 5),
        child: SelectableText.rich(
          minLines: 1,
          maxLines: param.maxLines,
          TextSpan(
            children: spans,
        ))
    );
  }
}

class SessionMessagePane extends StatelessWidget {
  final String message;
  final int maxLines;

  const SessionMessagePane({ required this.message, required this.maxLines, super.key});

  @override
  Widget build(BuildContext context) {
    List<String> contents = ImRichTextUtil.split(message);
    List<InlineSpan> spans = contents.map((content) {
      if (content.startsWith(ImRichTextUtil.SPLIT)) {
        var shortName = content.substring(3, content.length-1);
        var emoji = EmojiOneInfoes.getInstance().getByUnicodeOrShortName(shortName);
        return WidgetSpan(child: Image(image: AssetImage(emoji?.emojiPath??''), width: 15, height: 15,));
      } else {
        return TextSpan(text: content, style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE));
      }

    }).toList(growable: false);

    return Padding(
        padding: const EdgeInsets.only(top: 2, right: 5, bottom: 2, left: 5),
        child: Text.rich(
            // minLines: 1,
            maxLines: maxLines,
            TextSpan(
              children: spans,
            )
        )
    );
  }
}